#include "EmotionInterface.h"
#include "ColorfulLog.h"

/**********************************
 * 函数名：setGraph_visible
 * 功能：设置曲线是否显示
 * 输入：graphID：0:视频标准曲线，蓝色
 *               1:当前情绪曲线，橙色 由槽函数进行修改
 * 输出：无
 * 备注：无
**********************************/
void EmotionInterface::setGraph_visible(int graphID, bool flag){
    emo_plot->graph(graphID)->setVisible(flag);
}

void EmotionInterface::setCurrent_emotion(EmoMode mode){
    current_emotion = mode;
}
void EmotionInterface::setVideo_emotion(EmoMode mode){
    video_emotion = mode;
}

EmotionInterface::EmotionInterface(const QHash<QString, QLabel*> &uiLabelHash, QCustomPlot *emo_plot) :
    emo_plot(emo_plot),
    currentEmotion(current_emotion),
    videoEmotion(video_emotion)
{
    current_emotion = Neutral;
    video_emotion = Neutral;

    emo_negLabel = uiLabelHash.value("emo_negLabel");
    emo_neuLabel = uiLabelHash.value("emo_neuLabel");
    emo_posLabel = uiLabelHash.value("emo_posLabel");
    y_negLabel = uiLabelHash.value("y_negLabel");
    y_neuLabel = uiLabelHash.value("y_neuLabel");
    y_posLabel = uiLabelHash.value("y_posLabel");

    emotionGIFInit();
    emotionPlotInit();
}

EmotionInterface::~EmotionInterface()
{
}

/**********************************
 * 函数名：emotionImageInit
 * 功能：窗口初始化:image显示
 * 输入：无
 * 输出：无
 * 备注：无
**********************************/
void EmotionInterface::emotionImageInit(){
    QImage* img=new QImage;
    //加载图像
    // if(! ( img->load(":/pic/pic/1Negative.jpg") ) ) {
    if(! ( img->load(":/pic/pic/2Neutral.jpg") ) ) {
    // if(! ( img->load(":/pic/pic/3Positive.jpg") ) ) {
        // QMessageBox::information(SM_widget_ptr,
        //                          QObject::tr("打开图像失败"),
        //                          QObject::tr("打开图像失败!"));
        return ;
        delete img;
        // return;
    }
    QImage* imgScaled = new QImage;
    *imgScaled=img->scaled(200,
                           175,
                           Qt::KeepAspectRatio);
    // ui->emotionWidget->setPixmap(QPixmap::fromImage(*imgScaled));
    emo_negLabel->setPixmap(QPixmap::fromImage(*imgScaled));
    delete img;
    delete imgScaled;
}

/**********************************
 * 函数名：emotionGIFInit
 * 功能：窗口初始化:gif显示
 * 输入：无
 * 输出：无
 * 备注：无
**********************************/
void EmotionInterface::emotionGIFInit(){
    // QString gifPath = ":/pic/pic/Negative.gif";      
    QString gifPath = ":/pic/pic/Neutral.gif";
    // QString gifPath = ":/pic/pic/Positive.gif";

    emo_gif = new QMovie(gifPath);
    emo_neuLabel->setMovie(emo_gif);
    emo_gif->start();
    emo_neuLabel->setScaledContents(true);
    emo_gif->stop();
    

    // QEventLoop loop;//定义一个新的事件循环
    // QTimer::singleShot(1000, &loop, SLOT(quit()));//创建单次定时器，槽函数为事件循环的退出函数
    // loop.exec();//事件循环开始执行，程序会卡在这里，直到定时时间到，本循环被退出
}

/**********************************
 * 函数名：emotionPlotInit
 * 功能：情绪曲线初始化
 * 输入：无
 * 输出：无
 * 备注：无
**********************************/
void EmotionInterface::emotionPlotInit(){
    emo_plot->addGraph();
    emo_plot->graph(0)->setPen(QPen(QColor(40, 110, 255),3));//曲线1蓝色,video
    emo_plot->addGraph();
    emo_plot->graph(1)->setPen(QPen(QColor(255, 110, 40),3));//曲线2红色,current
    
    // y轴图例
    QString gifPath = ":/pic/pic/Positive.gif";
    QMovie movie(gifPath);
    movie.jumpToFrame(0);
    QImage resizedFrame = movie.currentImage().scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    QPixmap pixmap = QPixmap::fromImage(resizedFrame);
    y_posLabel->setPixmap(pixmap);
    y_posLabel->show();
    
    gifPath = ":/pic/pic/Neutral.gif";
    movie.setFileName(gifPath);
    movie.jumpToFrame(0);
    resizedFrame = movie.currentImage().scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    pixmap = QPixmap::fromImage(resizedFrame);
    y_neuLabel->setPixmap(pixmap);
    y_neuLabel->show();

    gifPath = ":/pic/pic/Negative.gif";
    movie.setFileName(gifPath);
    movie.jumpToFrame(0);
    resizedFrame = movie.currentImage().scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    pixmap = QPixmap::fromImage(resizedFrame);
    y_negLabel->setPixmap(pixmap);
    y_negLabel->show();

    emo_plot->graph(1)->setVisible(false);// 初始化隐藏曲线2
    // setGraph_visible(0,false);

    //坐标轴使用时间刻度
    // QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    // timeTicker->setTimeFormat("%h:%m:%s");
    // emo_plot->xAxis->setTicker(timeTicker);
    //四边安上坐标轴
    // emo_plot->axisRect()->setupFullAxesBox();
    //设置x,y轴范围
    emo_plot->xAxis->setRange(0, 10); // 从 0 到 2
    emo_plot->yAxis->setRange(-1.2, 1.2);
    // 隐藏y轴刻度值
    emo_plot->yAxis->setTickLabels(false);

    emo_plot->xAxis->grid()->setVisible(false);
    emo_plot->yAxis->grid()->setVisible(false);
    // emo_plot->xAxis->setTicks(false);
    emo_plot->yAxis->setTicks(false);

    emo_plot->setInteractions(QCP::iRangeDrag);

    // 背景透明
    emo_plot->setBackground(Qt::transparent);
    emo_plot->setStyleSheet("background: transparent;");


    // 使上下轴、左右轴范围同步
    // connect(emo_plot->xAxis, SIGNAL(rangeChanged(QCPRange)), emo_plot->xAxis2, SLOT(setRange(QCPRange)));
    // connect(emo_plot->yAxis, SIGNAL(rangeChanged(QCPRange)), emo_plot->yAxis2, SLOT(setRange(QCPRange)));
    //定时器连接槽函数realtimeDataSlot
    connect(&timer_emoPlot, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
    // timer_emoPlot.start(0); // 间隔时间 0ms表示尽可能快的触发
    // m_start_emoPlot = true;
}

/**********************************
 * 函数名：startPlot
 * 功能：情绪状态监测开始画线
 * 输入：无
 * 输出：无
 * 备注：无
**********************************/
void EmotionInterface::startPlot(const int &arg){
    timer_emoPlot.start(0);
    // m_start_emoPlot = true;
}
void EmotionInterface::stopPlot(const int &arg){
    timer_emoPlot.stop();
    // m_start_emoPlot = true;
}
void EmotionInterface::clearPlot(const int &arg){
    
}

/**********************************
 * 函数名：realtimeDataSlot
 * 功能：定时向曲线中添加点并刷新
 * 输入：无
 * 输出：无
 * 备注：无
**********************************/
void EmotionInterface::realtimeDataSlot()
{
    static QTime time(QTime::currentTime());

    double key = time.elapsed()/1000.0; // 开始到现在的时间，单位秒
    static double lastPointKey = 0;
    if (key-lastPointKey > 0.05) // 大约50ms添加一次数据
    {
        // 添加数据到graph
        // emo_plot->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
        // emo_plot->graph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
        emo_plot->graph(0)->addData(key, video_emotion-0.05);
        emo_plot->graph(1)->addData(key, current_emotion);
        //记录当前时刻
        lastPointKey = key;
    }
    // 曲线能动起来的关键在这里，设定x轴范围为最近8个时刻
    // emo_plot->xAxis->setRange(key, 8, Qt::AlignRight);
    emo_plot->xAxis->setRange(0, key);
    //绘图
    emo_plot->replot();
}

void EmotionInterface::set_emoGIF(){
    QString picPath = ":/pic/pic/";
    switch (current_emotion)
    {
        case -1:
        picPath.append("Negative.gif");
        break;

        case 0:
        picPath.append("Neutral.gif");
        break;

        case 1:
        picPath.append("Positive.gif");
        break;

        default:
            break;
    }

    // 切换图片
    emo_gif->stop();
    emo_gif->setFileName(picPath);
    emo_gif->start();
}

void EmotionInterface::hideEmoGIF(){
    emo_gif->stop();
    emo_neuLabel->hide();
}




